/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring.listener;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Shell;

import com.tmall.testgen.client.runinspring.RunInSpringUtil;
import com.tmall.testgen.plugin.views.vo.SpringTree;
import com.tmall.testgen.plugin.wizards.MyMessageDialog;
import com.tmall.testgen.plugin.wizards.dialog.MyInputDialog;

public class CodeInputListener implements  SelectionListener{
	TreeViewer parasTreeViewer;
	Shell shell;
	
	/**
	 * 手工输入框是否被打开
	 */
	private static boolean isOpen=false;
	
	public CodeInputListener(TreeViewer parasTreeViewer,Shell shell){
		this.parasTreeViewer=parasTreeViewer;
		this.shell=shell;
	}
	@Override
	public void widgetSelected(SelectionEvent e) {
		IStructuredSelection selection = (IStructuredSelection) parasTreeViewer
				.getSelection();
		SpringTree currentTree = (SpringTree) selection
				.getFirstElement();
		if(!currentTree.isEditable())
			return;
		
		if(isOpen){
		    MyMessageDialog.openConfirmDialog(shell, "您已经开打开了输入框，别打开那么多啊！");
		    return;
		}
		isOpen=true;
		
		MyInputDialog dialog = new MyInputDialog(shell, "手工输入框", "请输入全路径代码：", "",currentTree);
		int returnInt = dialog.open();
		
		isOpen=false;
		if(returnInt==Window.OK){
			//点击ok处理
			dealOk( currentTree, dialog);
		}
	}
	private void dealOk(SpringTree currentTree,MyInputDialog dialog){
		Object result =dialog.getResult();
		currentTree.removeAllChildren();
		
		currentTree.setClazz(result.getClass());
		currentTree.setLeafNode(false);
		currentTree.initName();
		RunInSpringUtil.addValue2Tree(currentTree, result);

		
		
		parasTreeViewer.refresh();
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
	}

}
